import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
	roles: ['admin','editor']    control the page roles (you can set multiple roles)
	title: 'title'               the name show in sidebar and breadcrumb (recommend set)
	icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
	breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
	activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
	{
		path: '/login',
		component: () => import('@/views/login/index'),
		hidden: true
	},

	{
		path: '/404',
		component: () => import('@/views/404'),
		hidden: true
	},

	{
		path: '/',
		component: Layout,
		redirect: '/dashboard',
		children: [{
			path: 'dashboard',
			name: 'Dashboard',
			component: () => import('@/views/dashboard/index'),
			meta: { title: '首页', icon: 'dashboard' }
		}]
	},

	// {
	//   path: '/example',
	//   component: Layout,
	//   redirect: '/example/table',
	//   name: 'Example',
	//   meta: { title: 'Example', icon: 'el-icon-s-help' },
	//   children: [
	//     {
	//       path: 'table',
	//       name: 'Table',
	//       component: () => import('@/views/table/index'),
	//       meta: { title: 'Table', icon: 'table' }
	//     },
	//     {
	//       path: 'tree',
	//       name: 'Tree',
	//       component: () => import('@/views/tree/index'),
	//       meta: { title: 'Tree', icon: 'tree' }
	//     }
	//   ]
	// },

	// {
	//   path: '/form',
	//   component: Layout,
	//   children: [
	//     {
	//       path: 'index',
	//       name: 'Form',
	//       component: () => import('@/views/form/index'),
	//       meta: { title: 'Form', icon: 'form' }
	//     }
	//   ]
	// }
]

/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 */
export const asyncRoutes = [
	{
		path: '/userList',
		component: Layout,
		children: [
			{
				path: 'index',
				name: 'userList',
				component: () => import('@/views/userList/index'),
				meta: { perms: ['GET /admin/useruserAll', 'GET /admin/useruserShare'], title: '会员统计', icon: 'form' }
			}
		]
	},
	{
		path: '/merchants',
		component: Layout,
		redirect: '/merchants/audit',
		meta: { title: '商家管理', icon: 'form' },
		alwaysShow: true,
		children: [
			{
				path: 'audit',
				name: 'audit',
				component: () => import('@/views/merchant/audit'),
				meta: { perms: ['GET /admin/brandbrandList', 'GET /admin/brandseleceAllBrand'], title: '入驻审核', icon: 'form' }
			},
			{
				path: 'detail',
				name: 'detail',
				component: () => import('@/views/merchant/detail'),
				meta: { perms: ['GET /admin/brand/brandInformation'], title: '商家信息', icon: 'form' }
			},
		]
	},
	{
		path: '/mall',
		component: Layout,
		redirect: '/mall/category',
		meta: { title: '商场管理', icon: 'form' },
		alwaysShow: true,
		children: [
			{
				path: 'category',
				name: 'category',
				component: () => import('@/views/mall/category'),
				meta: { perms: ['GET /admin/catalog/read'], title: '类目管理', icon: 'form' }
			},
		]
	},
	{
		path: '/promotion',
		component: Layout,
		redirect: '/promotion/coupon',
		meta: { title: '推广管理', icon: 'form' },
		alwaysShow: true,
		children: [
			{
				path: 'coupon',
				name: 'coupon',
				component: () => import('@/views/promotion/coupon'),
				meta: { perms: ['GET /admin/coupon/list'], title: '优惠券管理', icon: 'form' }
			},
			{
				path: 'couponDetail',
				name: 'couponDetail',
				component: () => import('@/views/promotion/couponDetail'),
				meta: { perms: ['GET /admin/coupon/read'], title: '优惠券详情', icon: 'form' },
				hidden: true
			},
		]
	},
	{
		path: '/goods',
		component: Layout,
		redirect: '/goods/index',
		children: [
			{
				path: 'index',
				name: 'goods',
				component: () => import('@/views/goods/index'),
				meta: { perms: ['GET /admin/brand/brandInformation', 'GET /admin/brandseleceAllBrand', 'GET /admin/brandbrandList'], title: '商品管理', icon: 'form' }
			}
		]
	},
	{
		path: '/order',
		component: Layout,
		redirect: '/order/index',
		children: [
			{
				path: 'index',
				name: 'order',
				component: () => import('@/views/order/index'),
				meta: { perms: ['POST /admin/orderselectBystatus'], title: '订单管理', icon: 'form' }
			}
		]
	},
	{
		path: '/service',
		component: Layout,
		redirect: '/service/ad',
		meta: {
			title: '服务管理',
			icon: 'form'
		},
		children: [
			{
				path: 'ad',
				name: 'ad',
				component: () => import('@/views/serviceManage/ad'),
				meta: { perms: ['GET /admin/advertrule/selectadvertrule'], title: '广告位', icon: 'form' }
			},
			{
				path: 'adOrder',
				name: 'adOrder',
				component: () => import('@/views/serviceManage/adOrder'),
				meta: { perms: ['GET /admin/advertorder/queryAdvertisement'], title: '广告位订单', icon: 'form' }
			},
		]
	},
	{
		path: '/approve',
		component: Layout,
		redirect: '/approve/captain',
		meta: {
			title: '审查审核',
			icon: 'form'
		},
		alwaysShow: true,
		children: [
			{
				path: 'captain',
				name: 'captain',
				component: () => import('@/views/approve/captain'),
				meta: { perms: ['GET /admin/siniHeadexamineHead'], title: '团长审核', icon: 'form' }
			},
		]
	},
	{
		path: '/withdraw',
		component: Layout,
		redirect: '/withdraw/merchant',
		meta: {
			title: '提现管理',
			icon: 'form'
		},
		alwaysShow: true,
		children: [
			{
				path: 'merchant',
				name: 'merchant',
				component: () => import('@/views/withdraw/merchant'),
				meta: { perms: ['GET /admin/appwith/selectwithByType'], title: '商家', icon: 'form' }
			},
		]
	},
	{
		path: '/system',
		component: Layout,
		redirect: '/system/manager',
		meta: {
			title: '系统设置',
			icon: 'form'
		},
		alwaysShow: true,
		children: [
			{
				path: 'manager',
				name: 'manager',
				component: () => import('@/views/system/manager'),
				meta: {
					perms: ['POST /admin/admin/update', 'POST /admin/admin/delete', 'POST /admin/admin/create', 'GET /admin/admin/read', 'GET /admin/admin/list'],
					title: '管理员',
					icon: 'form'
				}
			},
			{
				path: 'role',
				name: 'role',
				component: () => import('@/views/system/role'),
				meta: { perms: ['GET /admin/roles/list'],title: '角色管理', icon: 'form' }
			},
			{
				path: 'ladderList',
				name: 'ladderList',
				component: () => import('@/views/system/ladderList'),
				meta: { perms: ['GET /admin/WxdIfferPrice/selectDifferPrice'],title: '阶梯价格表', icon: 'form' }
			},
			{
				path: 'log',
				name: 'log',
				component: () => import('@/views/system/log'),
				meta: {title: '操作日志', icon: 'form' }
			},
		]
	},
	// 404 page must be placed at the end !!!
	{ path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
	// mode: 'history', // require service support
	scrollBehavior: () => ({ y: 0 }),
	routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
	const newRouter = createRouter()
	router.matcher = newRouter.matcher // reset router
}

export default router
